<?php
session_start();

if (!$_SESSION['loggedin']) {
    // User not logged in, redirect to login page
    echo "<script language='javascript'>window.location='index.php'</script>";
}
if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'registrar') {
?>
    <html>
        <head>
            <link rel="stylesheet" href="css/style.css" type="text/css" id="" media="print, projection, screen" />
            <script type="text/javascript" src="script/jquery-latest.js"></script>
            <script type="text/javascript" src="script/jquery.validate.js"></script>
            <script type="text/javascript">
                $(document).ready(function(){
                    $("#commentForm").validate({
                        rules: {
                            password: "required",
                            password_again: {
                                equalTo: "#password"
                            }
                        }
                    });
                });

                function confirmLogout()
                {
                    var agree=confirm("Are you sure you want to Logout?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmSubmit()
                {
                    var agree=confirm("Are you sure all fields entered are correct?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }
                function confirmReset()
                {
                    var agree=confirm("Are you sure you want to reset all fields?");
                    if (agree)
                        return true ;
                    else
                        return false ;
                }

            </script>

            <title>TUP Professor Management</title>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        </head>

        <body style=" height:100%; width: 100%; background-image: url(image/wall6.jpg);">

            <div id="container" style="height:auto; width: 100%;">

                <div style="background-image: url(image/wall12.jpg); height: 110px; width: auto; ">
                </div>

                <div align="center" style="background-image: url(image/wall11.jpg);  height: 74px; width: auto;">
                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMViewProf.php"><b>[ Professor Management ]</b></a>&nbsp;&nbsp;
                        <a href="SMMViewStud.php"><b>Student Management</b></a>&nbsp;&nbsp;
                        <a href="CMMViewCourse.php"><b>Course Management</b></a>&nbsp;&nbsp;
                        <a href="EMSelectProf.php"><b>Enrollment</b></a>
                    </div>

                    <div align="center" style=" height: 4px; width: auto;"></div>
                    <div align="center" style=" height: 22px; width: auto;">
                        <a href="TMMAddProf.php" >[ Add Professor ]</a>&nbsp;
                        <a href="TMMViewProf.php">View Professor</a>&nbsp;
                        <a href="TMMSearchProf.php">Search Professor</a>&nbsp;
                    </div>

                    <div  align="left" style="padding-left: 4%; height: 18px; width: 46%;float:left;"><?php echo "<aa class=\"smallw\"><b>Welcome " . $_SESSION['fname'] . ", Last login: " . $_SESSION['lastlogin'] . "</b></aa>"; ?>
                    </div>
                    <div align="right" style="padding-right: 4%; height: 18px; width: 46%;float:left;"><bb>|</bb>
                        <a class="small" href="RegEditProfile.php"><b>Edit Profile</b></a>
                        <bb>|</bb>
                        <a onclick="return confirmLogout()" class="small" href="Logout.php"><b>Logout</b></a>
                        <bb>|</bb></div>
                </div>
                <div align="left" style="padding-left: 4%; height: auto;  float:left; width: 96%;">

                    <h2>Add Professor</h2>

                <?php
                if (isset($_POST['Submit'])) {
                    $fname = $_POST['fname'];
                    $mname = $_POST['mname'];
                    $lname = $_POST['lname'];
                    $password = $_POST['password'];
                    $email = $_POST['email'];
                    $username = $_POST['username'];
                    $gender = $_POST['gender'];
                    $accounttype = "professor";
                    include 'DBLibrary/config.php';
                    include 'DBLibrary/openDB.php';

                    mysql_select_db($dbname);
                    $rowedit = mysql_fetch_array(mysql_query("SELECT DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 8 HOUR), '%y') as yyyy"));

                    $queryprofnum = "select * from user where accounttype='professor'";
                    $proftotal = mysql_query($queryprofnum);
                    $totalprof = mysql_num_rows($proftotal);
                    $idno = 'PROF' . $rowedit['yyyy'] . str_pad($totalprof + 1, 2, '0', STR_PAD_LEFT);



                    $query = "INSERT INTO user(username, accounttype, fname, mname, lname, gender, password, email, validate, idno)
                    VALUES('$username', '$accounttype' , '$fname', '$mname', '$lname', '$gender', '$password', '$email', 'activated','$idno' ) ";

                    $queryvalue = mysql_query($query);
                    if ($queryvalue == 0) {
                        echo "<a class=\"red\">" . mysql_errno($conn) . ": " . mysql_error($conn) . "</a>\n";
                        echo "<h3 class=\"err\">Input failed</h3>";
                    } else if ($queryvalue == 1)
                        echo "<h3 class=\"err\">Professor has been added</h3>";

                    include 'DBLibrary/closeDB.php';
                } ?>

                <h3>Professor Information</h3>
                <form onsubmit="return confirmSubmit()"class="cmxform" id="commentForm" method="post" action="TMMAddProf.php">
                    <p><br>Username :&nbsp;<input id="username" type="text" name="username" value="" size="16" maxlength="16" class="required" minlength="8"/><br>
                        <br>First Name MI. Last Name:&nbsp;<input id="fname" type="text" name="fname" value="" size="14" maxlength="32" class="required" minlength="1" />&nbsp;&nbsp;<input id="mname" type="text" name="mname" value="" size="1" maxlength="1" class="required" minlength="1"/>.&nbsp;&nbsp;<input id="lname" type="text" name="lname" value="" size="10" maxlength="32" class="required" minlength="1"/><br>
                        <br>*Gender: Male&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="gender" value="Male" class="required"/>
                        <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Female<input type="radio" name="gender" value="Female"/><br>

                        <br>Password:&nbsp;<input id="password" type="password" name="password" value="abcd1234" size="32" maxlength="32"  class="required" minlength="8"/><br>
                        <br>Email Address:&nbsp;<input id="email" type="text" name="email" value="" size="32" maxlength="64" class="required email" minlength="1"/><br>
                        <br><br><input name="Submit" class="submit" type="submit" value="Submit"/><input type="reset" onclick ="return confirmReset()" value="Reset"/>
                    </p></form>
            </div>
        </div>

        <div id="footer" style="background-image: url(image/wall11.jpg);clear:both;text-align:center; height: 26px; width:auto;">
            <div align="center" style=" height: 4px; width: auto;"></div>
            <bb class ="small">Copyright &#169; 2011</bb>
        </div>

    </body>
</html>
<?php
            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'admin') {
                echo "<script language='javascript'>window.location='UMMViewUser.php'</script>";
            } else if ($_SESSION['loggedin'] && $_SESSION['accounttype'] == 'professor') {
                echo "<script language='javascript'>window.location='AMMSelectSection.php'</script>";
            }
?>